

<!DOCTYPE html>
<html lang="zh" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/walker_sue/img/favicon.png">
  <link rel="icon" type="image/png" href="/walker_sue/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="lk">
  <meta name="keywords" content="">
  <title>时间序列分析（三） 周期因子 - Walker_Sue</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/walker_sue/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/github-gist.min.css" />
    
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/walker_sue/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/walker_sue/js/utils.js" ></script>
  <script  src="/walker_sue/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/walker_sue/">&nbsp;<strong>Walker</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/walker_sue/">
                <i class="iconfont icon-home-fill"></i>
                主页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/walker_sue/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/walker_sue/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/walker_sue/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/walker_sue/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-books"></i>
                文档
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/">
                    
                    主题博客
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/guide/">
                    
                    配置指南
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/icon/">
                    
                    图标用法
                  </a>
                
              </div>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/walker_sue/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
    <span class="h2" id="subtitle">
    
</span>




<div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-01-25 20:45" pubdate>
        January 25, 2021 pm
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.2k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      33
       分钟
    </span>
  

  
  
</div>


</div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">时间序列分析（三） 周期因子</h1>
            
            <div class="markdown-body" id="post-body">
              <div align='center' ><font size='10'>机器学习-BI</font></div>

<hr>
<div align='center' ><font size='5'>Week_03</font></div>
<div align='center' ><font size='5'>时间序列规则--周期因子</font></div>

<hr>
<h2 id="时间序列预测"><a href="#时间序列预测" class="headerlink" title="时间序列预测"></a>时间序列预测</h2><p>在对时间序列问题进行建模预测之前，通常可以通过一些简单的规则对结果进行提前的预测，可以作为baseline，供之后的模型进行参考。很多数据分析的比赛，都可以基于对于背景的理解和数据分析获得有用的规则，通过”if A then B”等方式设计出很好的基准方案。<br></p>
<blockquote>
<p>一般我们可以采取一些简单的统计量作为特征：</p>
<ul>
<li>中位数：较为稳健；</li>
</ul>
</blockquote>
<ul>
<li>均值：分布较符合正态分布时；</li>
<li>临近数据：临待遇测数据较近的数据；</li>
</ul>
<h3 id="01-基于周期因子的时间序列预测"><a href="#01-基于周期因子的时间序列预测" class="headerlink" title="01. 基于周期因子的时间序列预测"></a>01. 基于周期因子的时间序列预测</h3><p>支付数据、客流量数据、交通数据等时间序列，通常都具有比较明显的周期性，我们可以利用数据的周期性的变化，总结出简单的规则进行预测。<br></p>
<blockquote>
<p>其基本的步骤为：</p>
<ul>
<li>选择特征<br>可以用简单的统计量来作为特征，从中提取出有用的信息。<pre><code>1）中位数：居于中间位置的数，较为稳健
2）均值：当分布符合正态分布时，可以代表整体特征
3）临近数据：离待测数据越近的数据对其影响越大</code></pre>
</li>
</ul>
</blockquote>
<ul>
<li>确定组成一个周期的元素(1号~31号),一周或者一个月；</li>
<li>结合STL分解观察周期的变化；</li>
<li>基于周期因子的缺点是没有办法考虑到节假日与突发事件的影响，不过作为baseline还是可以有一个最为基本的判断。</li>
</ul>
<blockquote>
<p>以下为基于周期因子预测的一个简单的实例：</p>
</blockquote>
<br>

<table>
<thead>
<tr>
<th>星期</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
<th>周日</th>
<th>周均值</th>
</tr>
</thead>
<tbody><tr>
<td>week 1</td>
<td>20</td>
<td>10</td>
<td>70</td>
<td>50</td>
<td>250</td>
<td>200</td>
<td>100</td>
<td>100</td>
</tr>
<tr>
<td>week 2</td>
<td>26</td>
<td>18</td>
<td>66</td>
<td>50</td>
<td>180</td>
<td>140</td>
<td>80</td>
<td>80</td>
</tr>
<tr>
<td>week 3</td>
<td>15</td>
<td>8</td>
<td>67</td>
<td>60</td>
<td>270</td>
<td>160</td>
<td>120</td>
<td>100</td>
</tr>
</tbody></table>
<br>
通过给定前3周的数据，目标是预测第四周每天的客流量。可以发现，从周一到周日，数据存在明显的周期性波动，预测的主要目的就是尽量准确的将这种周期的波动提取出来。
<br>

<p><strong>&lt;1&gt; 获得周期因子</strong> <br><br><br></p>
<ul>
<li>方法1：除以周均值，之后按列取中位数； <br></li>
</ul>
<table>
<thead>
<tr>
<th>星期</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
<th>周日</th>
</tr>
</thead>
<tbody><tr>
<td>第一周</td>
<td>0.2</td>
<td>0.1</td>
<td>0.7</td>
<td>0.5</td>
<td>2.5</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>第二周</td>
<td>0.325</td>
<td>0.225</td>
<td>0.825</td>
<td>0.625</td>
<td>2.25</td>
<td>1.75</td>
<td>1</td>
</tr>
<tr>
<td>第三周</td>
<td>0.15</td>
<td>0.08</td>
<td>0.67</td>
<td>0.6</td>
<td>2.7</td>
<td>1.6</td>
<td>1.2</td>
</tr>
<tr>
<td>中位数</td>
<td>0.2</td>
<td>0.1</td>
<td>0.7</td>
<td>0.6</td>
<td>2.5</td>
<td>1.75</td>
<td>1</td>
</tr>
</tbody></table>
<br>
- 方法2：季节指数的计算方式，获得每日的均值，再除以整体均值；<br>

<table>
<thead>
<tr>
<th>星期</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
<th>周日</th>
<th>周均值</th>
</tr>
</thead>
<tbody><tr>
<td>week 1</td>
<td>20</td>
<td>10</td>
<td>70</td>
<td>50</td>
<td>250</td>
<td>200</td>
<td>100</td>
<td>100</td>
</tr>
<tr>
<td>week 2</td>
<td>26</td>
<td>18</td>
<td>66</td>
<td>50</td>
<td>180</td>
<td>140</td>
<td>80</td>
<td>80</td>
</tr>
<tr>
<td>week 3</td>
<td>15</td>
<td>8</td>
<td>67</td>
<td>60</td>
<td>270</td>
<td>160</td>
<td>120</td>
<td>100</td>
</tr>
<tr>
<td>均值</td>
<td>20.33</td>
<td>12</td>
<td>67.67</td>
<td>53.33</td>
<td>233.33</td>
<td>166.67</td>
<td>100</td>
<td>93.33</td>
</tr>
<tr>
<td>因子</td>
<td>0.22</td>
<td>0.13</td>
<td>0.73</td>
<td>0.57</td>
<td>2.50</td>
<td>1.79</td>
<td>1.07</td>
<td>1</td>
</tr>
</tbody></table>
<br>

<p>此外，由于中位数可能会丢失掉一定的信息，想要对周期因子进行优化，还可以通过提取均值与中位数，并按照一定的比例进行融合。 <br><br><br><br><strong>&lt;2&gt; 获得base预测</strong> <br><br>得到每周的周期因子之后，将这个因子乘以一个base，作为预测的结果。base的选择最简单的方法是直接选择最后一周的平均值，得到的结果如下：<br></p>
<table>
<thead>
<tr>
<th>星期</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
<th>周日</th>
</tr>
</thead>
<tbody><tr>
<td>中位数</td>
<td>0.2</td>
<td>0.1</td>
<td>0.7</td>
<td>0.6</td>
<td>2.5</td>
<td>1.75</td>
<td>1</td>
</tr>
<tr>
<td>预测 (base=100)</td>
<td>20</td>
<td>10</td>
<td>70</td>
<td>60</td>
<td>250</td>
<td>175</td>
<td>100</td>
</tr>
</tbody></table>
<br>
由于周期因素的存在，用统一的均值来作为base预测工作日和周末的客流量可能会不太准确，为了优化base的选择，我们可以将客流量去周期之后再取均值作为base。 <br>

<table>
<thead>
<tr>
<th>星期</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
<th>周日</th>
</tr>
</thead>
<tbody><tr>
<td>第三周</td>
<td>15</td>
<td>8</td>
<td>67</td>
<td>60</td>
<td>270</td>
<td>160</td>
<td>120</td>
</tr>
<tr>
<td>中位数</td>
<td>0.2</td>
<td>0.1</td>
<td>0.7</td>
<td>0.6</td>
<td>2.5</td>
<td>1.75</td>
<td>1</td>
</tr>
<tr>
<td>去周期以后的客流量</td>
<td>75</td>
<td>80</td>
<td>95</td>
<td>100</td>
<td>108</td>
<td>91.4</td>
<td>120</td>
</tr>
</tbody></table>
<br>


<h3 id="02-baseline-基于周期因子的预测"><a href="#02-baseline-基于周期因子的预测" class="headerlink" title="02 baseline 基于周期因子的预测"></a>02 baseline 基于周期因子的预测</h3><pre><code class="hljs python"><span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-comment"># 数据加载</span>
df = pd.read_csv(<span class="hljs-string">&quot;D:/Desktop/开课吧/BI学习/week2 时间序列实战/code&amp;data/资金流入流出预测/user_balance_table.csv&quot;</span>)
tf = df.loc[:,[<span class="hljs-string">&quot;report_date&quot;</span>,<span class="hljs-string">&quot;total_purchase_amt&quot;</span>,<span class="hljs-string">&quot;total_redeem_amt&quot;</span>]]
tf = tf.groupby(<span class="hljs-string">&quot;report_date&quot;</span>).<span class="hljs-built_in">sum</span>()
tf[<span class="hljs-string">&quot;report_date&quot;</span>] = tf.index
tf.index = <span class="hljs-built_in">range</span>(tf.shape[<span class="hljs-number">0</span>])
tf.head()

＃	total_purchase_amt	total_redeem_amt	report_date
<span class="hljs-number">0</span>	<span class="hljs-number">32488348</span>	<span class="hljs-number">5525022</span>	<span class="hljs-number">20130701</span>
<span class="hljs-number">1</span>	<span class="hljs-number">29037390</span>	<span class="hljs-number">2554548</span>	<span class="hljs-number">20130702</span>
<span class="hljs-number">2</span>	<span class="hljs-number">27270770</span>	<span class="hljs-number">5953867</span>	<span class="hljs-number">20130703</span>
<span class="hljs-number">3</span>	<span class="hljs-number">18321185</span>	<span class="hljs-number">6410729</span>	<span class="hljs-number">20130704</span>
<span class="hljs-number">4</span>	<span class="hljs-number">11648749</span>	<span class="hljs-number">2763587</span>	<span class="hljs-number">20130705</span>

<span class="hljs-comment"># 添加日期特征</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add_timestamp</span>(<span class="hljs-params">df</span>):</span>
    data = df.copy()
    data[<span class="hljs-string">&quot;report_date&quot;</span>] = pd.to_datetime(data[<span class="hljs-string">&quot;report_date&quot;</span>], <span class="hljs-built_in">format</span>=<span class="hljs-string">&quot;%Y%m%d&quot;</span>)
    <span class="hljs-comment"># 添加时间维度</span>
    data[<span class="hljs-string">&quot;day&quot;</span>] = data[<span class="hljs-string">&quot;report_date&quot;</span>].dt.day
    data[<span class="hljs-string">&quot;month&quot;</span>] = data[<span class="hljs-string">&quot;report_date&quot;</span>].dt.month
    data[<span class="hljs-string">&quot;year&quot;</span>] = data[<span class="hljs-string">&quot;report_date&quot;</span>].dt.year
    <span class="hljs-comment"># 一年中第几个week</span>
    data[<span class="hljs-string">&quot;week&quot;</span>] = data[<span class="hljs-string">&quot;report_date&quot;</span>].dt.week
    <span class="hljs-comment"># 周几</span>
    data[<span class="hljs-string">&quot;weekday&quot;</span>] = data[<span class="hljs-string">&quot;report_date&quot;</span>].dt.weekday
    <span class="hljs-keyword">return</span> data

data = add_timestamp(tf)

<span class="hljs-comment"># 生成预测数据</span>
colList = [<span class="hljs-string">&#x27;total_purchase_amt&#x27;</span>, <span class="hljs-string">&#x27;total_redeem_amt&#x27;</span>, <span class="hljs-string">&#x27;report_date&#x27;</span>]
test_data = pd.DataFrame(<span class="hljs-literal">None</span>,columns=colList)
test_data[<span class="hljs-string">&quot;report_date&quot;</span>] = pd.date_range(<span class="hljs-string">&quot;20140901&quot;</span>,<span class="hljs-string">&quot;20140930&quot;</span>,freq=<span class="hljs-string">&quot;D&quot;</span>)

<span class="hljs-comment"># 合并数据</span>
total_balance = pd.concat([data_use[colList],test_data],axis=<span class="hljs-number">0</span>)
total_balance.reset_index(inplace=<span class="hljs-literal">True</span>)
total_balance = add_timestamp(total_balance)
total_balance

＃	index	total_purchase_amt	total_redeem_amt	report_date	day	month	year	week	weekday
<span class="hljs-number">0</span>	<span class="hljs-number">0</span>	<span class="hljs-number">362865580</span>	<span class="hljs-number">211279011</span>	<span class="hljs-number">2014</span>-03-01	<span class="hljs-number">1</span>	<span class="hljs-number">3</span>	<span class="hljs-number">2014</span>	<span class="hljs-number">9</span>	<span class="hljs-number">5</span>
<span class="hljs-number">1</span>	<span class="hljs-number">1</span>	<span class="hljs-number">276202230</span>	<span class="hljs-number">246199417</span>	<span class="hljs-number">2014</span>-03-02	<span class="hljs-number">2</span>	<span class="hljs-number">3</span>	<span class="hljs-number">2014</span>	<span class="hljs-number">9</span>	<span class="hljs-number">6</span>
<span class="hljs-number">2</span>	<span class="hljs-number">2</span>	<span class="hljs-number">505305862</span>	<span class="hljs-number">513017360</span>	<span class="hljs-number">2014</span>-03-03	<span class="hljs-number">3</span>	<span class="hljs-number">3</span>	<span class="hljs-number">2014</span>	<span class="hljs-number">10</span>	<span class="hljs-number">0</span>
<span class="hljs-number">3</span>	<span class="hljs-number">3</span>	<span class="hljs-number">524146340</span>	<span class="hljs-number">250562978</span>	<span class="hljs-number">2014</span>-03-04	<span class="hljs-number">4</span>	<span class="hljs-number">3</span>	<span class="hljs-number">2014</span>	<span class="hljs-number">10</span>	<span class="hljs-number">1</span>
<span class="hljs-number">4</span>	<span class="hljs-number">4</span>	<span class="hljs-number">454295491</span>	<span class="hljs-number">209072753</span>	<span class="hljs-number">2014</span>-03-05	<span class="hljs-number">5</span>	<span class="hljs-number">3</span>	<span class="hljs-number">2014</span>	<span class="hljs-number">10</span>	<span class="hljs-number">2</span>
...	...	...	...	...	...	...	...	...	...
<span class="hljs-number">209</span>	<span class="hljs-number">25</span>	NaN	NaN	<span class="hljs-number">2014</span>-09-<span class="hljs-number">26</span>	<span class="hljs-number">26</span>	<span class="hljs-number">9</span>	<span class="hljs-number">2014</span>	<span class="hljs-number">39</span>	<span class="hljs-number">4</span>
<span class="hljs-number">210</span>	<span class="hljs-number">26</span>	NaN	NaN	<span class="hljs-number">2014</span>-09-<span class="hljs-number">27</span>	<span class="hljs-number">27</span>	<span class="hljs-number">9</span>	<span class="hljs-number">2014</span>	<span class="hljs-number">39</span>	<span class="hljs-number">5</span>
<span class="hljs-number">211</span>	<span class="hljs-number">27</span>	NaN	NaN	<span class="hljs-number">2014</span>-09-<span class="hljs-number">28</span>	<span class="hljs-number">28</span>	<span class="hljs-number">9</span>	<span class="hljs-number">2014</span>	<span class="hljs-number">39</span>	<span class="hljs-number">6</span>
<span class="hljs-number">212</span>	<span class="hljs-number">28</span>	NaN	NaN	<span class="hljs-number">2014</span>-09-<span class="hljs-number">29</span>	<span class="hljs-number">29</span>	<span class="hljs-number">9</span>	<span class="hljs-number">2014</span>	<span class="hljs-number">40</span>	<span class="hljs-number">0</span>
<span class="hljs-number">213</span>	<span class="hljs-number">29</span>	NaN	NaN	<span class="hljs-number">2014</span>-09-<span class="hljs-number">30</span>	<span class="hljs-number">30</span>	<span class="hljs-number">9</span>	<span class="hljs-number">2014</span>	<span class="hljs-number">40</span>	<span class="hljs-number">1</span>

<span class="hljs-comment"># 计算基于一个星期的周期因子</span>
tmp = total_balance.copy()
weekday = tmp[[<span class="hljs-string">&#x27;total_purchase_amt&#x27;</span>, <span class="hljs-string">&#x27;total_redeem_amt&#x27;</span>, <span class="hljs-string">&#x27;weekday&#x27;</span>]].dropna().astype(<span class="hljs-built_in">int</span>).groupby(<span class="hljs-string">&quot;weekday&quot;</span>,as_index=<span class="hljs-literal">False</span>).mean()
weekday.columns = [<span class="hljs-string">&#x27;weekday&#x27;</span>,<span class="hljs-string">&#x27;purchase_weekday&#x27;</span>, <span class="hljs-string">&#x27;redeem_weekday&#x27;</span>]

<span class="hljs-comment"># 计算基于星期的周期因子的baseline</span>
weekday[<span class="hljs-string">&#x27;purchase_weekday&#x27;</span>] /= data.total_purchase_amt.mean()
weekday[<span class="hljs-string">&#x27;redeem_weekday&#x27;</span>] /= data.total_redeem_amt.mean()

＃　	weekday	purchase_weekday	redeem_weekday
<span class="hljs-number">0</span>	<span class="hljs-number">0</span>	<span class="hljs-number">1.185411</span>	<span class="hljs-number">1.257964</span>
<span class="hljs-number">1</span>	<span class="hljs-number">1</span>	<span class="hljs-number">1.209608</span>	<span class="hljs-number">1.172248</span>
<span class="hljs-number">2</span>	<span class="hljs-number">2</span>	<span class="hljs-number">1.163752</span>	<span class="hljs-number">1.157944</span>
<span class="hljs-number">3</span>	<span class="hljs-number">3</span>	<span class="hljs-number">1.139503</span>	<span class="hljs-number">1.026762</span>
<span class="hljs-number">4</span>	<span class="hljs-number">4</span>	<span class="hljs-number">0.902682</span>	<span class="hljs-number">0.975445</span>
<span class="hljs-number">5</span>	<span class="hljs-number">5</span>	<span class="hljs-number">0.709237</span>	<span class="hljs-number">0.669791</span>
<span class="hljs-number">6</span>	<span class="hljs-number">6</span>	<span class="hljs-number">0.712065</span>	<span class="hljs-number">0.761712</span>

<span class="hljs-comment"># 计算日期因子</span>
<span class="hljs-comment"># 统计周一到周日，1-31号出现的频次 weekday,day 出现频次</span>
weekday_count = data_use[[<span class="hljs-string">&quot;report_date&quot;</span>, <span class="hljs-string">&quot;day&quot;</span>, <span class="hljs-string">&quot;weekday&quot;</span>]].groupby([<span class="hljs-string">&quot;day&quot;</span>, <span class="hljs-string">&quot;weekday&quot;</span>],as_index=<span class="hljs-literal">False</span>).count()
weekday_count.columns = [<span class="hljs-string">&quot;day&quot;</span>, <span class="hljs-string">&quot;weekday&quot;</span>, <span class="hljs-string">&quot;report_date_count&quot;</span>]
weekday_count
＃　day	weekday	report_date_count
<span class="hljs-number">0</span>	<span class="hljs-number">1</span>	<span class="hljs-number">1</span>	<span class="hljs-number">2</span>
<span class="hljs-number">1</span>	<span class="hljs-number">1</span>	<span class="hljs-number">3</span>	<span class="hljs-number">1</span>
<span class="hljs-number">2</span>	<span class="hljs-number">1</span>	<span class="hljs-number">4</span>	<span class="hljs-number">1</span>
<span class="hljs-number">3</span>	<span class="hljs-number">1</span>	<span class="hljs-number">5</span>	<span class="hljs-number">1</span>
<span class="hljs-number">4</span>	<span class="hljs-number">1</span>	<span class="hljs-number">6</span>	<span class="hljs-number">1</span>
...	...	...	...
<span class="hljs-number">149</span>	<span class="hljs-number">30</span>	<span class="hljs-number">6</span>	<span class="hljs-number">1</span>
<span class="hljs-number">150</span>	<span class="hljs-number">31</span>	<span class="hljs-number">0</span>	<span class="hljs-number">1</span>
<span class="hljs-number">151</span>	<span class="hljs-number">31</span>	<span class="hljs-number">3</span>	<span class="hljs-number">1</span>
<span class="hljs-number">152</span>	<span class="hljs-number">31</span>	<span class="hljs-number">5</span>	<span class="hljs-number">1</span>
<span class="hljs-number">153</span>	<span class="hljs-number">31</span>	<span class="hljs-number">6</span>	<span class="hljs-number">1</span>

weekday_day = pd.merge(weekday_count, weekday,on=<span class="hljs-string">&quot;weekday&quot;</span>)

<span class="hljs-comment"># 去除日期因子中weekday的影响</span>
weekday_day.purchase_weekday = weekday_day.purchase_weekday * weekday_day.report_date_count / <span class="hljs-built_in">len</span>(<span class="hljs-built_in">set</span>(data_use.month))
weekday_day.redeem_weekday = weekday_day.redeem_weekday * weekday_day.report_date_count / <span class="hljs-built_in">len</span>(<span class="hljs-built_in">set</span>(data_use.month))

<span class="hljs-comment"># 计算日期因子</span>
day_weight = weekday_day[[<span class="hljs-string">&quot;day&quot;</span>, <span class="hljs-string">&quot;purchase_weekday&quot;</span>, <span class="hljs-string">&quot;redeem_weekday&quot;</span>]].groupby(<span class="hljs-string">&quot;day&quot;</span>, as_index=<span class="hljs-literal">False</span>).<span class="hljs-built_in">sum</span>()
day_mean = data_use[[<span class="hljs-string">&quot;day&quot;</span>, <span class="hljs-string">&quot;total_purchase_amt&quot;</span>, <span class="hljs-string">&quot;total_redeem_amt&quot;</span>]].groupby(<span class="hljs-string">&quot;day&quot;</span>, as_index=<span class="hljs-literal">False</span>).mean()
day_mean = pd.merge(day_mean, day_weight, on=<span class="hljs-string">&quot;day&quot;</span>, how=<span class="hljs-string">&quot;left&quot;</span>)
day_base = day_mean.copy()
day_base.total_purchase_amt /= day_base.purchase_weekday
day_base.total_redeem_amt /= day_base.redeem_weekday
day_base

＃　	day	total_purchase_amt	total_redeem_amt	purchase_weekday	redeem_weekday
<span class="hljs-number">0</span>	<span class="hljs-number">1</span>	<span class="hljs-number">3.318261e+08</span>	<span class="hljs-number">2.437791e+08</span>	<span class="hljs-number">0.980451</span>	<span class="hljs-number">0.963034</span>
<span class="hljs-number">1</span>	<span class="hljs-number">2</span>	<span class="hljs-number">2.550589e+08</span>	<span class="hljs-number">2.193092e+08</span>	<span class="hljs-number">0.972816</span>	<span class="hljs-number">0.996800</span>
<span class="hljs-number">2</span>	<span class="hljs-number">3</span>	<span class="hljs-number">2.947971e+08</span>	<span class="hljs-number">3.010781e+08</span>	<span class="hljs-number">1.015888</span>	<span class="hljs-number">0.985873</span>
<span class="hljs-number">3</span>	<span class="hljs-number">4</span>	<span class="hljs-number">3.120176e+08</span>	<span class="hljs-number">2.785948e+08</span>	<span class="hljs-number">1.012700</span>	<span class="hljs-number">1.050126</span>
<span class="hljs-number">4</span>	<span class="hljs-number">5</span>	<span class="hljs-number">3.223549e+08</span>	<span class="hljs-number">2.571989e+08</span>	<span class="hljs-number">1.019458</span>	<span class="hljs-number">0.992417</span>
<span class="hljs-number">5</span>	<span class="hljs-number">6</span>	<span class="hljs-number">3.080774e+08</span>	<span class="hljs-number">2.521549e+08</span>	<span class="hljs-number">0.973279</span>	<span class="hljs-number">0.975970</span>

<span class="hljs-comment"># 数据预测</span>
<span class="hljs-comment">#　day_base 得到了按照日期计算相对纯净的日期total_purchase_amt和total_redeem_amt的均值【剔除了weekday的影响】</span>
<span class="hljs-comment"># 添加预测日期区间的weekday分布，和weekday权重,计算的到9月的预测值</span>
day_pred = pd.merge(day_base, add_timestamp(test_data)[[<span class="hljs-string">&quot;report_date&quot;</span>,<span class="hljs-string">&quot;day&quot;</span>,<span class="hljs-string">&quot;weekday&quot;</span>]], on=<span class="hljs-string">&quot;day&quot;</span>, how=<span class="hljs-string">&quot;left&quot;</span>)
day_pred.total_purchase_amt *= day_pred.purchase_weekday
day_pred.total_redeem_amt *= day_pred.redeem_weekday
day_pred

＃　day	total_purchase_amt	total_redeem_amt	purchase_weekday	redeem_weekday	report_date	weekday
<span class="hljs-number">0</span>	<span class="hljs-number">1</span>	<span class="hljs-number">3.253391e+08</span>	<span class="hljs-number">2.347676e+08</span>	<span class="hljs-number">0.980451</span>	<span class="hljs-number">0.963034</span>	<span class="hljs-number">2014</span>-09-01	<span class="hljs-number">0.0</span>
<span class="hljs-number">1</span>	<span class="hljs-number">2</span>	<span class="hljs-number">2.481255e+08</span>	<span class="hljs-number">2.186074e+08</span>	<span class="hljs-number">0.972816</span>	<span class="hljs-number">0.996800</span>	<span class="hljs-number">2014</span>-09-02	<span class="hljs-number">1.0</span>
<span class="hljs-number">2</span>	<span class="hljs-number">3</span>	<span class="hljs-number">2.994808e+08</span>	<span class="hljs-number">2.968247e+08</span>	<span class="hljs-number">1.015888</span>	<span class="hljs-number">0.985873</span>	<span class="hljs-number">2014</span>-09-03	<span class="hljs-number">2.0</span>
<span class="hljs-number">3</span>	<span class="hljs-number">4</span>	<span class="hljs-number">3.159802e+08</span>	<span class="hljs-number">2.925598e+08</span>	<span class="hljs-number">1.012700</span>	<span class="hljs-number">1.050126</span>	<span class="hljs-number">2014</span>-09-04	<span class="hljs-number">3.0</span>
<span class="hljs-number">4</span>	<span class="hljs-number">5</span>	<span class="hljs-number">3.286272e+08</span>	<span class="hljs-number">2.552484e+08</span>	<span class="hljs-number">1.019458</span>	<span class="hljs-number">0.992417</span>	<span class="hljs-number">2014</span>-09-05	<span class="hljs-number">4.0</span>
<span class="hljs-number">5</span>	<span class="hljs-number">6</span>	<span class="hljs-number">2.998453e+08</span>	<span class="hljs-number">2.460958e+08</span>	<span class="hljs-number">0.973279</span>	<span class="hljs-number">0.975970</span>	<span class="hljs-number">2014</span>-09-06	<span class="hljs-number">5.0</span>

<span class="hljs-comment"># 结果保存输出</span>
res = pd.DataFrame(<span class="hljs-literal">None</span>,columns=colList)
time2str = <span class="hljs-keyword">lambda</span> x:<span class="hljs-built_in">str</span>(x).replace(<span class="hljs-string">&quot;-&quot;</span>,<span class="hljs-string">&quot;&quot;</span>)[:<span class="hljs-number">8</span>]
res[<span class="hljs-string">&quot;report_date&quot;</span>] = day_pred[<span class="hljs-string">&quot;report_date&quot;</span>].apply(time2str)
res[<span class="hljs-string">&quot;total_purchase_amt&quot;</span>] = day_pred[<span class="hljs-string">&quot;total_purchase_amt&quot;</span>].astype(<span class="hljs-built_in">int</span>)
res[<span class="hljs-string">&quot;total_redeem_amt&quot;</span>] = day_pred[<span class="hljs-string">&quot;total_redeem_amt&quot;</span>].astype(<span class="hljs-built_in">int</span>)
res.to_csv(<span class="hljs-string">&quot;submit_res_date.csv&quot;</span>,header=<span class="hljs-literal">None</span>,index=<span class="hljs-literal">None</span>)</code></pre>
<h3 id="03-节假日判断"><a href="#03-节假日判断" class="headerlink" title="03 节假日判断"></a>03 节假日判断</h3><p>可以使用python包chinese_calendar的特征。<br></p>
<ul>
<li>安装</li>
</ul>
<pre><code class="hljs python">pip install chinesecalendar</code></pre>
<ul>
<li>使用</li>
</ul>
<pre><code class="hljs python"><span class="hljs-keyword">import</span> datetime
<span class="hljs-keyword">from</span> chinese_calendar <span class="hljs-keyword">import</span> is_workday, is_holiday
temp  =  datetime.date(<span class="hljs-number">2021</span>,<span class="hljs-number">1</span>,<span class="hljs-number">10</span>)
print(is_workday(temp))
print(is_holiday(temp))</code></pre>


            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/walker_sue/categories/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/">机器学习</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/walker_sue/tags/BI/">BI</a>
                    
                      <a class="hover-with-bg" href="/walker_sue/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/">机器学习</a>
                    
                      <a class="hover-with-bg" href="/walker_sue/tags/%E6%97%B6%E9%97%B4%E5%BA%8F%E5%88%97%E5%88%86%E6%9E%90/">时间序列分析</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/walker_sue/2021/01/28/week3-wordtoken/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">新闻自动化处理（一）</span>
                        <span class="visible-mobile">Vorheriger</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/walker_sue/2021/01/19/BI-week2-Prophet/">
                        <span class="hidden-mobile">时间序列分析（二）Prophet</span>
                        <span class="visible-mobile">Nächster</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;TOC</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">Suchen</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">Stichwort</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/walker_sue/js/debouncer.js" ></script>
<script  src="/walker_sue/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/walker_sue/js/lazyload.js" ></script>
  



  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/walker_sue/js/clipboard-use.js" ></script>







  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    function typing(id, title){
        var typed = new Typed('#' + id, {
            strings: [
              '  ',
              title + "&nbsp;",
            ],
            cursorChar: "_",
            typeSpeed: 70,
            loop: false,
        });
        typed.stop();
        $(document).ready(function () {
            $(".typed-cursor").addClass("h2");
            typed.start();
        });
    }
    
        typing("subtitle", "时间序列分析（三） 周期因子")
    
  </script>


  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/walker_sue/js/local-search.js" ></script>
  <script>
    var path = "/walker_sue/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>





  

  
    <!-- MathJax -->
    <script>
      MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']]
        },
        options: {
          renderActions: {
            findScript: [10, doc => {
              document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
                const display = !!node.type.match(/; *mode=display/);
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
                const text = document.createTextNode('');
                node.parentNode.replaceChild(text, node);
                math.start = { node: text, delim: '', n: 0 };
                math.end = { node: text, delim: '', n: 0 };
                doc.math.push(math);
              });
            }, '', false],
            insertedScript: [200, () => {
              document.querySelectorAll('mjx-container').forEach(node => {
                let target = node.parentNode;
                if (target.nodeName.toLowerCase() === 'li') {
                  target.parentNode.classList.add('has-jax');
                }
              });
            }, '', false]
          }
        }
      };
    </script>

    <script async src="https://cdn.staticfile.org/mathjax/3.0.5/es5/tex-svg.js" ></script>

  











</body>
</html>
